package dtm;

import tm.TMConfiguration;
import tm.TMRule;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haoly
 * @title DTMRulebook
 * @date 2025/4/23 14:23
 * @description 确定性图灵机规则集
 */
public class DTMRulebook<T> {
    private List<TMRule<T>> rules;

    public DTMRulebook(List<TMRule<T>> rules) {
        this.rules = new ArrayList<>(rules);
    }

    public TMConfiguration<T> nextConfiguration(TMConfiguration<T> configuration){
        TMRule<T> applicableRule = ruleFor(configuration);
        if (applicableRule != null) {
            return applicableRule.follow(configuration);
        }
        return null;
    }

    public TMRule<T> ruleFor(TMConfiguration<T> configuration) {
        for (TMRule<T> rule : rules) {
            if (rule.appliesTo(configuration)) {
                return rule;
            }
        }

        return null;
    }

    public Boolean appliesTo(TMConfiguration<T> configuration){
        TMRule<T> applicableRule = ruleFor(configuration);
        return applicableRule != null;
    }

    public List<TMRule<T>> getRules() {
        return rules;
    }
}
